#!/bin/bash
__SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
if [ ! -e "$__SCRIPT_DIR/utils.sh" ]
then
    echo "[ERR] $__SCRIPT_DIR/utils.sh not found"
    exit -1
fi

source "$__SCRIPT_DIR/utils.sh"

function prepare_c() {
    _solution_name=${_SOLUTION_BASENAME}.cpp
    [ -e "$1/${_solution_name}" ] && return
    cat <<EOF > $1/${_solution_name}
#include "leetcode.h"

class Solution
{
public:
    void solution() {
        // your code here
    }
    void test() {
        // test code
        printf("Test Done!\n");
    }
};

int main(int argc, char *argv[]) {
#ifndef NDEBUG
    printf("[MAIN] argc: %d\n", argc);
    printf("[MAIN] argv: ");
    for (int i = 0; i < argc; ++i) {
        printf(argv[i]);
        if (i == argc - 1) {
            printf("\n");
        } else {
            printf(" ");
        }
    }
#endif
    Solution().test();
}

EOF
}

function prepare_py() {
    _solution_name=${_SOLUTION_BASENAME}.py
    [ -e "$1/${_solution_name}" ] && return
    cat <<EOF > $1/${_solution_name}
#!/usr/bin/env python
# coding: utf-8

class Solution(object):
    @staticmethod
    def solution():
        # your code here

    @staticmethod
    def test():
        # test code
        print("Test Done!")

if __name__ == "__main__":
    Solution.test()

EOF
}

function prepare_go() {
    _solution_name=${_SOLUTION_BASENAME}.go
    [ -e "$1/${_solution_name}" ] && return
    cat <<EOF > $1/${_solution_name}
package leetcode

func Solution() {
    // your code here
}

EOF
    _solution_test_name=${_SOLUTION_BASENAME}_test.go
    [ -e "$1/${_solution_test_name}" ] && return
    cat <<EOF > $1/${_solution_test_name}
package leetcode

import (
    "gotest.tools/assert"
    "testing"
)

func TestSolution(t *testing.T) {
    // test code
    fmt.Println("Test Done!")
}

EOF
}

function main() {
    problem_name=${_PROBLEM_NAME// /_}
    if [ -n "$problem_name" ]
    then
        for p in "$problem_name/c++" "$problem_name/python" "$problem_name/golang"
        do
            [ -e "$p" ] || mkdir -p "$p" || die "fail to create '$p'"
        done
        prepare_c "$problem_name/c++"
        prepare_py "$problem_name/python"
        prepare_go "$problem_name/golang"
        find $problem_name -name "solution.*"
    fi
}

_PROBLEM_NAME="$@"
main
